<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
    <head th:replace="common :: header"/>
<body>
<div class="page">
	<div class="card">
		<div class="card-body dm-table0">
			<div id="table"></div>
            <div id="toolbar" style="display: none">
                <a shiro:hasPermission="system:dept:add" href="#" onclick="add()" class="easyui-linkbutton"
                   data-options="iconCls:'icon-add',plain:true">增加</a>
                <a shiro:hasPermission="system:dept:edit" href="#" onclick="edit()" class="easyui-linkbutton"
                   data-options="iconCls:'icon-edit',plain:true">修改</a>
                <a shiro:hasPermission="system:dept:delete" href="#" onclick="remove()" class="easyui-linkbutton"
                   data-options="iconCls:'icon-cancel',plain:true">删除</a>
                <a href="#" class="easyui-linkbutton" id="btnCollapse" title="折叠" data-options="iconCls:'icon-set',plain:true">折叠</a>
                <a href="#" class="easyui-linkbutton" id="btnExpand" title="展开" data-options="iconCls:'icon-set',plain:true" style="display: none">展开</a>
                <input id="btnShowDisabled">
            </div>
        </div>
	</div>
</div>
<script type="text/javascript">
$(function() {
	$('#table').treegrid({
	    url: '/system/dept/list',
        queryParams: {status: 0},
	    idField: 'deptId',
	    treeField: 'deptName',
	    fit: true,
        fitColumns: true,
        singleSelect: true,
        toolbar: '#toolbar',
        onLoadSuccess: function(){
            $('#table').treegrid('clearSelections');
        },
        columns:[ [
			{field: 'ck', checkbox: true },
			{title: '名称', field:'deptName', width:200, align:'left'},
            {title: '备注', field:'remark', width:400, align:'left'},
			{title: '状态',field: 'status',width: 60, align:'center',formatter: statusFormatter}
	    ] ]
    })
});

function add() {
    var row = $('#table').treegrid("getSelected");
    if (!row) {
        dm.openDialog('新增部门', '/system/dept/add?parentId=0', 500, 340);
    } else if(row.status==1){
        dm.msgWarning("所选部门已停用，不能再添加子部门！");
    } else {
        dm.openDialog('新增部门', '/system/dept/add?parentId='+row.deptId, 500, 340);
    }
}
function edit() {
    dm.getRow('table', function(row) {
        dm.openDialog('修改部门','/system/dept/edit?deptId='+row.deptId,500,390);
    })
}

function remove() {
    dm.getRow('table',function(row) {
        dm.confirm('删除操作不可恢复，且删除后，部门下的所有子部门都将被删除。您确定要删除该部门吗？', function () {
            dm.post('/system/dept/delete?deptId=' + row.deptId, {}, function () {
                dm.search('table', null, 'treegrid');
                $('#btnCollapse').show();
                $('#btnExpand').hide();
            })
        })
    })
}

$('#btnExpand').click(function() {
    $('#table').treegrid('expandAll');
    $(this).hide();
    $('#btnCollapse').show();
});

$('#btnCollapse').click(function() {
    $('#table').treegrid('collapseAll');
    $(this).hide();
    $('#btnExpand').show();
});

$('#btnShowDisabled').checkbox({
    label:'显示停用',
    onChange:function (checked) {
        var params = checked ? {status: -1} : {status: 0};
        dm.search('table', params, 'treegrid');
    }
});

</script>
</body>
</html>
